
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author PhongLan
 */
public class DataImportance {

    public DataImportance(String sName) {
        REC_STORE = sName;
    }
    private RecordStore rs = null;
    private String REC_STORE = "dbImportance";

    private void db(String str) {
        System.err.println("Msg: " + str);
    }

    public void close() {
        try {
            rs.closeRecordStore();
        } catch (Exception e) {
            db(e.toString());
        }
    }

    public void open() throws RecordStoreException {
        try {
            rs = RecordStore.openRecordStore(REC_STORE, true);
        } catch (RecordStoreException e) {
            db(e.toString());
        }
    }

    public void delete() {
        if (RecordStore.listRecordStores() != null) {
            try {
                RecordStore.deleteRecordStore(REC_STORE);
            } catch (Exception e) {
                db(e.toString());
            }
        }
    }
    // ID, Name, Color, Volume, Music 

    public void insert(RecordImportance recordImportance) {
        try {
            ByteArrayOutputStream strmBytes = new ByteArrayOutputStream();
            DataOutputStream strmDataType = new DataOutputStream(strmBytes);
            byte[] buffer = null;
            strmDataType.writeUTF(recordImportance.sName);
            strmDataType.writeInt(recordImportance.iColor);
            strmDataType.writeInt(recordImportance.iVolume);
            strmDataType.writeUTF(recordImportance.sMusic);
            strmDataType.flush();
            buffer = strmBytes.toByteArray();
            rs.addRecord(buffer, 0, buffer.length);
            strmBytes.reset();
            strmBytes.close();
            strmDataType.close();
        } catch (Exception e) {
            db(e.toString());
        }
    }
    // ID, Name, Color, Volume, Music 

    public void update(int iIndex, RecordImportance recordImportance) {
        try {
            // Write data into an internal byte array
            ByteArrayOutputStream strmBytes = new ByteArrayOutputStream();
            DataOutputStream strmDataType = new DataOutputStream(strmBytes);
            byte[] buffer;
            strmDataType.writeUTF(recordImportance.sName);
            strmDataType.writeInt(recordImportance.iColor);
            strmDataType.writeInt(recordImportance.iVolume);
            strmDataType.writeUTF(recordImportance.sMusic);
            strmDataType.flush(); // Clear any buffered data
            buffer = strmBytes.toByteArray(); // Get stream data into byte array and write RecordImportance
            rs.setRecord(iIndex, buffer, 0, buffer.length);
            strmBytes.reset();
            strmBytes.close();
            strmDataType.close();
        } catch (Exception e) {
            db(e.toString());
        }
    }

    public void delete(int iIndex) {
        try {
            rs.deleteRecord(iIndex);
        } catch (Exception e) {
            db(e.toString());
        }
    }
    // ID, Name, Color, Volume, Music 

    public long[] selectTimes() {
        long[] arTimes = null;
        try {
            byte[] recData = new byte[50];
            ByteArrayInputStream strmBytes = new ByteArrayInputStream(recData);
            DataInputStream strmDataType = new DataInputStream(strmBytes);
            arTimes = new long[rs.getNumRecords()];
            for (int i = 0; i < rs.getNumRecords(); i++) {
                rs.getRecord(i + 1, recData, 0);
                arTimes[i] = strmDataType.readLong();
                strmBytes.reset();
            }
            strmBytes.close();
            strmDataType.close();
        } catch (Exception e) {
            db(e.toString());
        }
        return arTimes;
    }
    // ID, Name, Color, Volume, Music 

    public RecordImportance[] selectRecordImportances() {
        RecordImportance[] arRecordImportances = null;
        try {
            byte[] recData = new byte[50];
            ByteArrayInputStream strmBytes = new ByteArrayInputStream(recData);
            DataInputStream strmDataType = new DataInputStream(strmBytes);
            arRecordImportances = new RecordImportance[rs.getNumRecords()];
            for (int i = 0; i < rs.getNumRecords(); i++) {
                rs.getRecord(i + 1, recData, 0);
                arRecordImportances[i].sName = strmDataType.readUTF();
                arRecordImportances[i].iColor = strmDataType.readInt();
                arRecordImportances[i].iVolume = strmDataType.readInt();
                arRecordImportances[i].sMusic = strmDataType.readUTF();
                strmBytes.reset();
                // Reset so read starts at beginning of array 			
            }
            strmBytes.close();
            strmDataType.close();
        } catch (Exception e) {
            db(e.toString());
        }
        return arRecordImportances;
    }

    public RecordImportance selectRecordImportance(int iIndex) {
        RecordImportance RecordImportance = null;
        try {
            // Careful: Make sure this is big enough!Better yet, test and reallocate if necessary	
            byte[] recData = new byte[50];
            // Read from the specified byte array	
            ByteArrayInputStream strmBytes = new ByteArrayInputStream(recData);
            // Read Java data types from the above byte array	
            DataInputStream strmDataType = new DataInputStream(strmBytes);
            rs.getRecord(iIndex, recData, 0); // Read back the data types 			    	           
            RecordImportance recordImportance = new RecordImportance();
            recordImportance.sName = strmDataType.readUTF();
            recordImportance.iColor = strmDataType.readInt();
            recordImportance.iVolume = strmDataType.readInt();
            recordImportance.sMusic = strmDataType.readUTF();
            strmBytes.reset();
            // Reset so read starts at beginning of array 			
            strmBytes.close();
            strmDataType.close();
        } catch (Exception e) {
            db(e.toString());
        }
        return RecordImportance;
    }
}
